package jp.sinya.register.dao;

import jp.sinya.register.util.JdbcUtils3;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;

import java.sql.Connection;
import java.sql.SQLException;

public class MyQueryRunner extends QueryRunner {
    @Override
    public int[] batch(String sql, Object[][] params) throws SQLException {
        /*
        1得到连接
        2执行父类方法，传递连接对象
        3释放连接
        4返回值
         */

        Connection connection = JdbcUtils3.getConnection();
        int[] result = super.batch(connection, sql, params);
        JdbcUtils3.releaseConnection(connection);

        return result;
    }

    @Override
    public <T> T query(String sql, Object param, ResultSetHandler<T> rsh) throws SQLException {
        Connection connection = JdbcUtils3.getConnection();
        T result = super.query(connection, sql, param, rsh);
        JdbcUtils3.releaseConnection(connection);
        return result;
    }

    @Override
    public <T> T query(String sql, Object[] params, ResultSetHandler<T> rsh) throws SQLException {
        Connection connection = JdbcUtils3.getConnection();
        T result = super.query(connection, sql, params, rsh);
        JdbcUtils3.releaseConnection(connection);
        return result;
    }

    @Override
    public <T> T query(String sql, ResultSetHandler<T> rsh, Object... params) throws SQLException {
        Connection connection = JdbcUtils3.getConnection();
        T result = super.query(connection, sql, rsh, params);
        JdbcUtils3.releaseConnection(connection);
        return result;
    }

    @Override
    public <T> T query(String sql, ResultSetHandler<T> rsh) throws SQLException {
        Connection connection = JdbcUtils3.getConnection();
        T result = super.query(connection, sql, rsh);
        JdbcUtils3.releaseConnection(connection);
        return result;
    }

    @Override
    public int update(String sql) throws SQLException {
        Connection connection = JdbcUtils3.getConnection();
        int result = super.update(connection, sql);
        JdbcUtils3.releaseConnection(connection);
        return result;
    }

    @Override
    public int update(String sql, Object param) throws SQLException {
        Connection connection = JdbcUtils3.getConnection();
        int result = super.update(connection, sql, param);
        JdbcUtils3.releaseConnection(connection);
        return result;
    }

    @Override
    public int update(String sql, Object... params) throws SQLException {
        Connection connection = JdbcUtils3.getConnection();
        int result = super.update(connection, sql, params);
        JdbcUtils3.releaseConnection(connection);
        return result;
    }

}
